<template>
  <teleport to="body">
    <div v-if="visible" class="global-message-container">
      <transition-group name="message" tag="div" class="message-list">
        <div
          v-for="message in messages"
          :key="message.id"
          :class="[
            'message-item',
            `message-${message.type}`,
            { 'message-closable': message.closable }
          ]"
        >
          <div class="message-icon">
            <el-icon>
              <component :is="getIcon(message.type)" />
            </el-icon>
          </div>
          <div class="message-content">
            <div v-if="message.title" class="message-title">{{ message.title }}</div>
            <div class="message-text">{{ message.message }}</div>
          </div>
          <div v-if="message.closable" class="message-close" @click="closeMessage(message.id)">
            <el-icon><Close /></el-icon>
          </div>
        </div>
      </transition-group>
    </div>
  </teleport>
</template>

<script setup>
import { ref, computed } from 'vue'
import {
  CircleCheckFilled,
  WarningFilled,
  CircleCloseFilled,
  InfoFilled,
  Close
} from '@element-plus/icons-vue'

// 响应式数据
const messages = ref([])

// 计算属性
const visible = computed(() => messages.value.length > 0)

// 图标映射
const iconMap = {
  success: CircleCheckFilled,
  warning: WarningFilled,
  error: CircleCloseFilled,
  info: InfoFilled
}

// 方法
const getIcon = (type) => {
  return iconMap[type] || InfoFilled
}

const addMessage = (options) => {
  const id = Date.now().toString()
  const message = {
    id,
    type: options.type || 'info',
    title: options.title,
    message: options.message,
    closable: options.closable !== false,
    duration: options.duration || 3000
  }
  
  messages.value.push(message)
  
  // 自动关闭
  if (message.duration > 0) {
    setTimeout(() => {
      closeMessage(id)
    }, message.duration)
  }
  
  return id
}

const closeMessage = (id) => {
  const index = messages.value.findIndex(msg => msg.id === id)
  if (index > -1) {
    messages.value.splice(index, 1)
  }
}

const clearAll = () => {
  messages.value = []
}

// 暴露方法
defineExpose({
  addMessage,
  closeMessage,
  clearAll
})
</script>

<style lang="scss" scoped>
.global-message-container {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 3000;
  pointer-events: none;
}

.message-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.message-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  min-width: 320px;
  max-width: 480px;
  padding: 16px 20px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  pointer-events: auto;
  border-left: 4px solid;
  
  &.message-success {
    border-left-color: var(--el-color-success);
    
    .message-icon {
      color: var(--el-color-success);
    }
  }
  
  &.message-warning {
    border-left-color: var(--el-color-warning);
    
    .message-icon {
      color: var(--el-color-warning);
    }
  }
  
  &.message-error {
    border-left-color: var(--el-color-danger);
    
    .message-icon {
      color: var(--el-color-danger);
    }
  }
  
  &.message-info {
    border-left-color: var(--el-color-info);
    
    .message-icon {
      color: var(--el-color-info);
    }
  }
}

.message-icon {
  font-size: 18px;
  margin-top: 2px;
}

.message-content {
  flex: 1;
  
  .message-title {
    font-weight: 600;
    margin-bottom: 4px;
    color: var(--el-text-color-primary);
  }
  
  .message-text {
    color: var(--el-text-color-regular);
    line-height: 1.5;
  }
}

.message-close {
  cursor: pointer;
  color: var(--el-text-color-placeholder);
  transition: color 0.3s ease;
  
  &:hover {
    color: var(--el-text-color-regular);
  }
}

// 动画
.message-enter-active,
.message-leave-active {
  transition: all 0.3s ease;
}

.message-enter-from {
  opacity: 0;
  transform: translateY(-20px);
}

.message-leave-to {
  opacity: 0;
  transform: translateX(100%);
}

.message-move {
  transition: transform 0.3s ease;
}

// 响应式
@media (max-width: 768px) {
  .global-message-container {
    left: 20px;
    right: 20px;
    transform: none;
  }
  
  .message-item {
    min-width: auto;
    max-width: none;
  }
}
</style>
